from mqttdemo.mqtt2.mqtt import MqttSubscriber
from mqttdemo.mqtt1.mqttutil import MqttCallback


class MyCallback(MqttCallback):
    def on_connect(self, client, userdata, flags, rc):
        if rc == 0:
            print("Connected")
            client.subscribe("test/topic")

    def on_message(self, client, userdata, msg):
        print(f"Received: {msg.payload.decode()} on {msg.topic} 111")

callback = MyCallback()
subscriber = MqttSubscriber("localhost", client_id="SubscriberClient", callback=callback)
subscriber.connect()
subscriber.loop_start()

try:
    while True:
        pass
except KeyboardInterrupt:
    subscriber.loop_stop()
    subscriber.disconnect()
